Ext.define('PJ.view.messagenotification.NewsList', {
    extend: 'AM.default.DefaultList',
    requires: [],
    alias: ['widget.newsList'],
    isHavePage: false,
    selModel: {
        selType: '',
        // 设置为只能通过复选框选中行
        checkOnly: true
    },
    title: '最新通知',
    flag:"",
    initComponent: function(){
    	var me = this;
        me.columns = [{
    		header: '通知标题',
    		dataIndex: 'newsTitle',
    		flex: 1,
            minWidth: 120,
            renderer: function(value, meta, record){
            	meta.tdAttr = "newsId='" + record.get("newsId") + "'newsTypeCode='"+record.get("newsTypeCode")+"'className='"+record.get("className")+"'";
            	if (record.get("readState") == 1)
            		meta.style = "white-space:normal;cursor: pointer;color:blue;";
            	if (record.get("readState") == 2)
            		meta.style = "white-space:normal;cursor: pointer;color:red;";
            	return value;
            },
    		listeners: {
    			click: function(g, clm){
    				if(me.flag != "zw"){
    					var newsId = clm.getAttribute("newsId");
    					var win = Ext.create("PJ.view.messagemanagement.View", {
    						title: "查看通知",
    						text:"接收通知",
    						grid:me
    					}).show();
    					var actionName = "load";
    					if(gj_onlineLearning_constant.loadType == "jsonp"){
    						actionName = "loadJsonp";
    					}
    					win.down("form").getForm().doAction(actionName,{
    						url:me.store.preUpdateUrl,
    						params:{
    							newsId: newsId
    						},
    						success:function(f,action){
    							var data = action.response.responseText;
    							var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    							var form = win.down('form');	 
    							var baseForm = form.getForm();
    							if(json.data.newsTypeCode=="xxlx_pxb"){
    								baseForm.findField("specialName").setVisible(true);
    								baseForm.findField("specialName").setValue(json.data.className);
    								win.setHeight(360);
    							}else{
    								baseForm.findField("specialName").setVisible(false);
    								win.setHeight(320);
    							}
    							//实时
//    							Ext.Ajax.request({
//    								url: showMsgUrl,
////  								url: 'workbench/orchid/data/msgData.jsons',
//    								params: {},
//    								success: function(response){
//    									var data = response.responseText;
//    									var jsonData = Ext.decode(data, true);
//    									var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//    									var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//    									//me.setTitle("提醒管理("+remindUserNoReadCount+")")
//    									Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>';	
//    									Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>';
//    									$("#newRemind")[0].innerHTML="未读通知"+newsUserNoReadCount+"条，未读提醒"+remindUserNoReadCount+"条&nbsp&nbsp&nbsp&nbsp&nbsp";
//    								}
//    							});
    							me.store.load(function(rec, operation, success){
    								if(rec.length == 0){
    									me.flag = "zw";
    									var model = Ext.create("PJ.model.messagemanagement.ReceiveMessagesList");
    									model.set({
    										"newsTitle": "暂无通知"
    									});
    									rec.push(model);
    									model.commit();//提交model，如果添加此句则列表上不会再显示左上角修改后的标识符
    									store.add(rec)
    								}
    							});
//  							baseForm.findField("newsType").setValue(json.data.newsType.dataName);
    							//取到值后直接传给model
    							var model = Ext.create("PJ.model.messagemanagement.AttachmentList");
    							model.set({
    								"attachmentId": json.data.attachmentId,
    								"attachName": json.data.attachName,
    								"attachType":  json.data.attachType,
    								"fileSize": json.data.fileSize
    							});
    							var record = new Array();
    							var store = form.down("attachmentList").getStore();
    							record.push(model);
    							store.add(record);
    						}
    					});

    				}
    			}
    		}
    	}, {
            header: '通知类型',
            dataIndex: 'newsType',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			return value;
    		}
        }, {
            header: '发送人',
            dataIndex: 'userName',
    		flex: .5,
            width:150,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			return value;
    		}
        },{
            header: '发送时间',
            dataIndex: 'sendTime',
            width: 120 
        }, {
            header: '读取状态',
            dataIndex: 'readState',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			if(value==1){
    				return "<span style='color:blue'>已读</span>"
    			}else if(value==2){
    				return "<span style='color:red'>未读</span>"
    			}
    		}
        },/* {
            header: '是否回复',
            dataIndex: 'erplyState',
            width:120,
    		renderer: function(value, metaData, record){
    			metaData.tdAttr = 'data-qtip="' + value + '"';
    			if(value==1){
    				return "是"
    			}else if(value==2){
    				return "否"
    			}
    		}
        },*/{
            header: '回复',
            menuDisabled: true,
            sortable: false,
            resizable: false,
            hideable: false,
            xtype: 'actioncolumn',
            width: 60,
            items: [{
            	tooltip: '回复',
            	getClass: function(v, meta, rec){
            		meta.style = "cursor: pointer;";
            		if(me.flag !="zw"){
            			return 'templateIco';
            		}
        		},
                handler: function(grid, rowIndex, colIndex, item, eve, record){
                	var newsId = record.get("newsId");
                	var newsUserLink = record.get("newsUserLink");
    				var win = Ext.create("PJ.view.messagemanagement.ReplyEdit", {
            			title: "回复内容",
            			text:"接收通知",
            			grid:grid,
            			newsId:newsId,
            			newsUserLink:newsUserLink
            		}).show();
    				var actionName = "load";
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					actionName = "loadJsonp";
    				}
    				win.down("form").getForm().doAction(actionName,{
    					url:me.store.preUpdateUrl,
    					params:{
    						newsId: newsId
    					},
    					success:function(f,action){
//    						Ext.Ajax.request({
//    	                		url: showMsgUrl,
////    	              		url: 'workbench/orchid/data/msgData.jsons',
//    	                		params: {},
//    	                		success: function(response){
//    	                			var data = response.responseText;
//    	                			var jsonData = Ext.decode(data, true);
//    	                			var remindUserNoReadCount = jsonData.data.remindUserNoReadCount;
//    	                			var newsUserNoReadCount = jsonData.data.newsUserNoReadCount;
//    	                			//me.setTitle("提醒管理("+remindUserNoReadCount+")")
//    	                			 Ext.get("li_"+"8a10e7f9557b199401557b37dfbf0001").dom.innerHTML = '<span class="title"><b class="bull"></b>通知管理('+newsUserNoReadCount+')</span>'	
//									 Ext.get("li_"+"4028809c52207e8a015220860c5r0002").dom.innerHTML = '<span class="title"><b class="bull"></b>提醒管理('+remindUserNoReadCount+')</span>'	
//									 $("#newRemind")[0].innerHTML="未读通知"+newsUserNoReadCount+"条，未读提醒"+remindUserNoReadCount+"条&nbsp&nbsp&nbsp&nbsp&nbsp";
//    	                		}
//    	                	});
    						var data = action.response.responseText;
    	        			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
//    	        			var newsUserLink = json.data.newsUserLink;
    	        			var form = win.down('form');	 
    	        			var baseForm = form.getForm();
    	        			if(json.data.newsTypeCode=="xxlx_pxb"){
    	        				baseForm.findField("specialName").setVisible(true);
    	        				baseForm.findField("specialName").setValue(json.data.className);
    	        				win.setHeight(570);
    	        			}else{
    	        				baseForm.findField("specialName").setVisible(false);
    	        				win.setHeight(540);
    	        			}
    	        			var list = win.down("restoredMessageList");
    	        			list.newsUserLink = newsUserLink;
    	        			var store = list.store;
    	                    var params = store.proxy.extraParams;
    	                    var newParams = {
    	                    		searchNewsId: newsId,
    	                    		searchNewsUserLink: newsUserLink
    	                    };
    	                    Ext.apply(params, newParams);
    	                    store.isLoad = true;
    	                    store.load(function(rec, operation, success){
    	                    	if(rec.length == 0){
    	                    		me.flag = "zw";
    	                    		var model = Ext.create("PJ.model.messagemanagement.ReceiveMessagesList");
    	                    		model.set({
    	                    			"newsTitle": "暂无通知"
    	                    		});
    	                    		rec.push(model);
    	                    		model.commit();//提交model，如果添加此句则列表上不会再显示左上角修改后的标识符
    	                    		store.add(rec)
    	                    	}
    	                  });
    					}
    				});
                }
            },{
            	getClass: function(v, meta, rec){
            		if(rec.get("isNewReply")==1){
            			return 'newSignIco';
            		}
        		}
            }]
        }];
        me.store = Ext.create('PJ.'+ prototype +'.messagemanagement.ReceiveMessagesList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
    listLoad:function(){
    	var me = this;
        var store = this.store;
        var params = store.proxy.extraParams;
        var newParams = {
        		limit: 5
        };
        Ext.apply(params, newParams);
        store.isLoad = true;
        store.load(function(rec, operation, success){
        	if(rec.length == 0){
        		me.flag = "zw";
        		var model = Ext.create("PJ.model.messagemanagement.ReceiveMessagesList");
        		model.set({
        			"newsTitle": "暂无通知"
        		});
        		rec.push(model);
        		model.commit();//提交model，如果添加此句则列表上不会再显示左上角修改后的标识符
        		store.add(rec)
        	}
        });
	},
    listeners: {
    	viewready: function(){
    		var me = this;
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {
            		limit: 5
            };
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load(function(rec, operation, success){
            	if(rec.length == 0){
            		me.flag = "zw";
            		var model = Ext.create("PJ.model.messagemanagement.ReceiveMessagesList");
            		model.set({
            			"newsTitle": "暂无通知"
            		});
            		rec.push(model);
            		model.commit();//提交model，如果添加此句则列表上不会再显示左上角修改后的标识符
            		store.add(rec)
            	}
            });
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		me.timer = setInterval(function(){
    			me.listLoad();
    		},300000);
    		
    	}
    }
});
